﻿using Interface.Core.Controllers;
using Interface.Core.Utility;
using Interface.Mvc.Areas.Scrm.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Interface.Utility.Helper;
using DapperExtensions;

namespace Interface.Mvc.Areas.Scrm.Controllers
{
    public class AddressController : CoreController
    {
        /// <summary>
        /// 根据会员编号获取收货地址
        /// url: /scrm/address/query
        /// params: memberid
        /// </summary>
        [HttpGet]
        public string Query(int memberid)
        {
            var predicate = Predicates.Field<MemberAddress>(f => f.MemberID, Operator.Eq, memberid);

            var list = db.Scrm.GetList<MemberAddress>(predicate);

            return ResultUtility.SuccessResult(data: list);
        }

        /// <summary>
        /// 获取会员默认收获地址
        /// url: /scrm/address/getdefault
        /// params: memberid
        /// </summary>
        [HttpGet]
        public string GetDefault(int memberid)
        {
            string sql = @"Select ID,MemberID,Name,Mobile,ProvinceID,ProvinceName,
            CityID,CityName,CountyID,CountyName,Detail,IsDefault,IncludeDate 
            From Member_Address(nolock) Where MemberID=@MemberID And IsDefault=1";

            var entity = db.Scrm.Query<MemberAddress>(sql, new { MemberID = memberid }).FirstOrDefault();

            if (entity != null)
            {
                return ResultUtility.SuccessResult(data: entity);
            }

            return ResultUtility.FailureResult();
        }

        /// <summary>
        /// 设置默认收货地址
        /// url: /scrm/address/setdefault
        /// params: memberid, id
        /// </summary>
        [HttpPost]
        public string SetDefault(int memberid, int id)
        {
            if (memberid == 0) return ResultUtility.FailureResult();

            db.Scrm.BeginTransaction();

            db.Scrm.Execute("update Member_Address set IsDefault = 0 where MemberID = @MemberID", new { MemberID = memberid });

            int result = db.Scrm.Execute("update Member_Address set IsDefault = 1 where ID = @ID", new { ID = id });

            db.Scrm.Commit();

            if (result > 0)
            {
                return ResultUtility.SuccessResult();
            }

            return ResultUtility.FailureResult();
        }

        /// <summary>
        /// 添加收货地址
        /// url: /scrm/address/insert
        /// params: memberid, name, mobile, provinceid, provincename, cityid, cityname, countyid, countyname, detail, isdefault
        /// </summary>
        [HttpPost]
        public string Insert()
        {
            var result = 0;
            var entity = new MemberAddress().FormFill();

            entity.IncludeDate = DateTime.Now;

            if (entity.IsDefault)
            {
                db.Scrm.BeginTransaction();

                db.Scrm.Execute("update Member_Address set IsDefault = 0 where MemberID = @MemberID",
                    new { MemberID = entity.MemberID });

                result = db.Scrm.Insert(entity);

                db.Scrm.Commit();
            }
            else
            {
                result = db.Scrm.Insert(entity);
            }

            if (result > 0)
            {
                return ResultUtility.SuccessResult();
            }

            return ResultUtility.FailureResult();
        }

        /// <summary>
        /// 修改收货地址
        /// url: /scrm/address/update
        /// params: id, memberid, name, mobile, provinceid, provincename, cityid, cityname, countyid, countyname, detail, isdefault
        /// </summary>
        [HttpPost]
        public string Update()
        {
            var entity = new MemberAddress().FormFill();

            entity.IncludeDate = DateTime.Now;

            var result = db.Scrm.Update(entity);

            if (result)
            {
                return ResultUtility.SuccessResult();
            }

            return ResultUtility.FailureResult();
        }

        /// <summary>
        /// 删除收货地址
        /// url: /scrm/address/delete
        /// params: id
        /// params: memberid, id, isDefault
        /// </summary>
        [HttpPost]
        public string Delete(int memberid, int id, bool isDefault)
        {
            if (memberid == 0) return ResultUtility.FailureResult();

            bool success = false;
            var predicate = Predicates.Field<MemberAddress>(f => f.ID, Operator.Eq, id);

            if (!isDefault)
            {
                success = db.Scrm.Delete<MemberAddress>(predicate);
            }
            else
            {
                db.Scrm.BeginTransaction();

                success = db.Scrm.Delete<MemberAddress>(predicate);

                var entity = db.Scrm.GetList<MemberAddress>(Predicates.Field<MemberAddress>(f => f.MemberID, Operator.Eq, memberid)).FirstOrDefault();

                if (entity != null)
                {
                    db.Scrm.Execute("update Member_Address set IsDefault = 1 where ID = @ID", new { ID = entity.ID });
                }

                db.Scrm.Commit();
            }

            if (success)
            {
                return ResultUtility.SuccessResult();
            }

            return ResultUtility.FailureResult();
        }
    }
}